package com.xy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tencentcloudapi.iai.v20200303.models.Candidate;
import com.tencentcloudapi.iai.v20200303.models.Result;
import com.tencentcloudapi.iai.v20200303.models.SearchPersonsResponse;
import com.xy.constants.ControlCharacter;
import com.xy.entity.ViewerEntity;
import com.xy.request.*;
import com.xy.response.InspectResponse;
import com.xy.service.IInspectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 核验相关类
 */
@RestController
@AllArgsConstructor
@RequestMapping("inspect")
@Slf4j
@Api(value = "核验管理", tags = "核验管理")
public class InspectController extends BladeController {
	private final IInspectService iInspectService;

	/**
	 * 身份证鉴权
	 *
	 * @param idCardRequest
	 * @return
	 */
	@PostMapping("/id-card")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "身份证鉴权", notes = "传入idCardDTO")
	public R<InspectResponse> idCard(@Valid @RequestBody IdCardRequest idCardRequest) {
		ViewerEntity viewerEntity = BeanUtil.copy(idCardRequest, ViewerEntity.class);
		InspectResponse inspectResponse = iInspectService.oauthInspect(viewerEntity, idCardRequest, ControlCharacter.OAUTH_TYPE_ID_NUMBER);
		inspectResponse.setRecordViewer(idCardRequest.getIdCard());
		inspectResponse.setOauthType(ControlCharacter.OAUTH_TYPE_ID_NUMBER);
		return R.data(inspectResponse);
	}

	/**
	 * 二维码识别
	 *
	 * @param twoCodeRequset
	 * @return
	 */
	@PostMapping("/two-code")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "二维码识别", notes = "传入 qrCodeDTO")
	public R<InspectResponse> qrCode(@Valid @RequestBody TwoCodeRequset twoCodeRequset) {
		ViewerEntity viewerEntity = BeanUtil.copy(twoCodeRequset, ViewerEntity.class);
		InspectResponse inspectResponse = iInspectService.oauthInspect(viewerEntity, twoCodeRequset, ControlCharacter.OAUTH_TYPE_CARD_NO);
		inspectResponse.setRecordViewer(twoCodeRequset.getTwoCode());
		inspectResponse.setOauthType(ControlCharacter.OAUTH_TYPE_CARD_NO);
		return R.data(inspectResponse);
	}

	/**
	 * IC卡识别
	 *
	 * @param icRequest
	 * @return
	 */
	@PostMapping("/Ic")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "IC卡识别", notes = "传入 icRequest")
	public R<InspectResponse> rfid(@Valid @RequestBody ICRequest icRequest) {
		ViewerEntity viewerEntity = BeanUtil.copy(icRequest, ViewerEntity.class);
		InspectResponse inspectResponse = iInspectService.oauthInspect(viewerEntity, icRequest, ControlCharacter.OAUTH_TYPE_SWIPE_CARD);
		inspectResponse.setRecordViewer(icRequest.getSwipeCard());
		inspectResponse.setOauthType(ControlCharacter.OAUTH_TYPE_SWIPE_CARD);
		return R.data(inspectResponse);
	}

	/**
	 * 人脸识别
	 *
	 * @return
	 */
	@PostMapping("/face")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "人脸识别", notes = "传入face ")
	public R<InspectResponse> face(@Valid @RequestBody FaceDetectionRequest faceDetectionRequest) {
		ViewerEntity viewerEntity = iInspectService.face(faceDetectionRequest);
		InspectResponse inspectResponse = iInspectService.oauthInspect(viewerEntity, faceDetectionRequest, ControlCharacter.OAUTH_TYPE_FACE);
		inspectResponse.setRecordViewer(viewerEntity.getIdCard());
		inspectResponse.setOauthType(ControlCharacter.OAUTH_TYPE_FACE);
		return R.data(inspectResponse);
	}

	/**
	 * 人脸注册
	 *
	 * @param faceDetectionRequest
	 * @return
	 */
	@PostMapping("/face-register")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "人脸注册", notes = "传入 face")
	public R<InspectResponse> faceRegister(@Valid @RequestBody FaceDetectionRequest faceDetectionRequest) {
		return R.status(iInspectService.facefFaceRegister(faceDetectionRequest));
	}

	/**
	 * 保存门禁记录
	 *
	 * @param
	 * @return
	 */
	@PostMapping("/record-save")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "保存门禁记录", notes = "传入 TerRecordDTO")
	public R recordSave(@Valid @RequestBody GeneralRecordRequest generalRecordRequest) {
		return R.status(iInspectService.recordSave(generalRecordRequest));
	}

	/**
	 * 非正常门禁记录
	 *
	 * @param generalRecordRequest
	 * @return
	 */
	@PostMapping("/illegal-record-save")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "非法保存门禁记录", notes = "传入 illegalDTO")
	public R illegalRecordSave(@Valid @RequestBody GeneralRecordRequest generalRecordRequest) {
		return R.status(iInspectService.abnormalRecordSave(generalRecordRequest));
	}
}
